﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;
using System.Windows.Forms;

namespace MMS.Validation
{
    public static class ValidateFormUserInfo 
    {
        private static string blank = "";
        public static bool validateUI(FormUserInfo f)
        {
            bool finalResult = false;
            bool isValidName = true;
            bool isValidEmail = true;
            bool isValidPhone = true;
            bool isValidUserName = true;
            bool isValidPassword = true;


            //validate Name of User

            if (f.txtName.Text.Equals(blank) == true || ValidateUtil.isValidTextOnly(f.txtName.Text) == false)
            {
                f.txtName.BackColor = Color.Red;
                f.istxtNameActive = true;
                isValidName = false;

            }

            //validate Email
            if (f.txtEmail.Text.Equals(blank) == true || ValidateUtil.isValidEmail(f.txtEmail.Text) == false)
            {
                f.txtEmail.BackColor = Color.Red;
                f.istxtEmailActive = true;
                isValidEmail = false;
            }

            //validate Phone
            if (f.txtPhone.Text.Equals(blank) == true || ValidateUtil.isValidNum(f.txtPhone.Text) == false)
            {
                f.txtPhone.BackColor = Color.Red;
                f.istxtPhoneActive = true;
                isValidPhone = false;
            }

            //validate Username
            if (f.txtUserName.Text.Equals(blank) == true || ValidateUtil.isValidTextAndNum(f.txtUserName.Text) == false)
            {
                f.txtUserName.BackColor = Color.Red;
                f.istxtUserNameActive = true;
                isValidUserName = false;
            }
            //validate password
            if (f.txtPassword.Text.Equals(blank) == true || f.txtRepeatPassword.Text.Equals(blank) == true
                || f.txtPassword.Text.Equals(f.txtRepeatPassword.Text) == false)
            {
                isValidPassword = false;
                f.txtPassword.BackColor = Color.Red;
                f.txtRepeatPassword.BackColor = Color.Red;
                f.txtPassword.Text = "";
                f.txtRepeatPassword.Text = "";
                f.txtPassword.Focus();
                f.istxtPasswordActive = true;
            }

            finalResult = isValidName && isValidEmail && isValidPhone && isValidUserName && isValidPassword;
            return finalResult;
        }


        public static void ValidatingFormUserInfo(object sender, System.ComponentModel.CancelEventArgs CancelE, FormUserInfo f, DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider dxErrorProviderFormUserInfo)
        {
            
            string controlName = ((TextEdit)sender).Name;
            string strValidate = "";
            //validate txtName text box
            if (controlName.Equals(f.txtName.Name))
            {
                strValidate = f.txtName.Text.Trim();
                if (strValidate.Equals(blank) == true)
                {
                    CancelE.Cancel = true;
                }
            }
            else if (controlName.Equals(f.txtPhone.Name))
            { //validate Phone
                strValidate = f.txtPhone.Text.Trim();
                if (strValidate.Equals(blank) == true)
                {
                    CancelE.Cancel = true;
                }
            }

            else if (controlName.Equals(f.txtEmail.Name))
            {
                //validate Email
                strValidate = f.txtEmail.Text.Trim();
                if (strValidate.Equals(blank) == true || ValidateUtil.isValidEmail(strValidate) == false)
                {
                    CancelE.Cancel = true;
                }
                // dxErrorProviderFormUserInfo.ClearErrors(true);
            }
            else if (controlName.Equals(f.txtUserName.Name))
            {
                strValidate = f.txtUserName.Text.Trim();
                if (strValidate.Equals(blank) == true)
                {
                    CancelE.Cancel = true;
                }

            }



        }



        public static void InvalidValue_FormUserInfo(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs InvalidEx, FormUserInfo f, DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider dxErrorProviderFormUserInfo)
        {
            string controlName = ((TextEdit)sender).Name;
            InvalidEx.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
            //validate txtName text box
            if (controlName.Equals(f.txtName.Name))
            {
                // dxErrorProviderFormUserInfo.SetError(f.txtName, MessageBundle.ERROR_INVALID_NOTNULL_OR_TEXTONLY.Value, ErrorType.Default);
            }
            else if (controlName.Equals(f.txtPhone.Name))
            { //validate Phone
                //  dxErrorProviderFormUserInfo.SetError(f.txtPhone, MessageBundle.ERROR_INVALID_NOTNULL_OR_NUMONLY.Value, ErrorType.Default);
            }

            else if (controlName.Equals(f.txtEmail.Name))
            {
                //validate Email
                // dxErrorProviderFormUserInfo.SetError(f.txtEmail, MessageBundle.ERROR_INVALID_NOTNULL_OR_VALIDEMAIL.Value, ErrorType.Default);
            }
            else if (controlName.Equals(f.txtUserName.Name))
            {
                //  dxErrorProviderFormUserInfo.SetError(f.txtUserName, MessageBundle.ERROR_INVALID_NOTNULL_OR_TEXTANDNUMONLY.Value, ErrorType.Default);

            }


        }


        public static void ValidatingKeyPressFormUserInfo(object sender, KeyPressEventArgs e, FormUserInfo f)
        {
            string controlName = ((TextEdit)sender).Name;
            //validate txtName text box
            if (controlName.Equals(f.txtName.Name))
            {
                ValidateUtil.isLetterAndSpace(e);
            }
            else if (controlName.Equals(f.txtPhone.Name))
            { //validate Phone
                ValidateUtil.isDigit(e);
            }


            else if (controlName.Equals(f.txtUserName.Name))
            {
                ValidateUtil.IsLetterOrDigit(e);

            }

        }

       

        internal static bool isEmpty(FormUserInfo f)
        {
            bool result = false;
            if (f.txtName.Text.Equals(blank)
                || (f.txtEmail.Text.Equals(blank)
                 || f.txtPhone.Text.Equals(blank)
                        || f.txtUserName.Text.Equals(blank) == true))
            {
                result = true;
            }
            return result;
        }
    }
}
